﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


/// <summary>
/// 文件名：ClsAutoGeneNoDAL.cs
/// 作  者：Kidy
/// 时  间：2006.12.20
/// 说  明：ClsAutoGeneNoDAL类用于识别码管理信息相关处理
/// </summary>
public class ClsAutoGeneNoDAL
{

    private static string strSqlConnectionString = ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"];

    /// <summary>
    /// 自动产生识别码(批量增加)
    /// <returns>返回是否成功</returns>
    /// strPrefix,strSuffix,iBeginNO,iEndNo
    public Boolean AutoGenarationIdentify(string sPrefix, string sSuffix, int iBeginNO, int iEndNo,
        string sBodNo, DateTime dBodDate, string sIm_no, string sBatch_no)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AutoGenerationNo", con);
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@prefix", SqlDbType.NVarChar, 30);
        com.Parameters["@prefix"].Value = sPrefix;
        com.Parameters.Add("@suffix", SqlDbType.NVarChar, 30);
        com.Parameters["@suffix"].Value = sSuffix;
        com.Parameters.Add("@ibegin", SqlDbType.Int);
        com.Parameters["@ibegin"].Value = iBeginNO;
        com.Parameters.Add("@iend", SqlDbType.Int);
        com.Parameters["@iend"].Value = iEndNo;

        com.Parameters.Add("@bodNo", SqlDbType.NVarChar, 30);
        com.Parameters["@bodNo"].Value = sBodNo;
        com.Parameters.Add("@bodDate", SqlDbType.DateTime, 8);
        com.Parameters["@bodDate"].Value = dBodDate;
        com.Parameters.Add("@im_no", SqlDbType.NVarChar, 30);
        com.Parameters["@im_no"].Value = sIm_no;
        com.Parameters.Add("@batch_no", SqlDbType.NVarChar, 30);
        com.Parameters["@batch_no"].Value = sBatch_no;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 手动输入识别码（单条录入）
    /// <returns>返回是否成功</returns>
    /// strSinIdeNo
    public Boolean AddSingleIdentify(string sSinIdeNo, string sBodNo, DateTime dBodDate, string sIm_no, string sBatch_no)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddSingleIdentify", con);
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@SinIdeNo", SqlDbType.NVarChar, 30);
        com.Parameters["@SinIdeNo"].Value = sSinIdeNo;
        com.Parameters.Add("@bodNo", SqlDbType.NVarChar, 30);
        com.Parameters["@bodNo"].Value = sBodNo;
        com.Parameters.Add("@bodDate", SqlDbType.DateTime, 8);
        com.Parameters["@bodDate"].Value = dBodDate;
        com.Parameters.Add("@im_no", SqlDbType.NVarChar, 30);
        com.Parameters["@im_no"].Value = sIm_no;
        com.Parameters.Add("@batch_no", SqlDbType.NVarChar, 30);
        com.Parameters["@batch_no"].Value = sBatch_no;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 获取所有产生的识别码
    /// </summary>
    public DataSet GetIdentifyNo(int iFlag)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        //SqlCommand com = new SqlCommand("Pro_getAutoGeneIdentifyNo", con);
        //com.CommandType = CommandType.StoredProcedure;

        SqlDataAdapter dat = new SqlDataAdapter("Pro_getAutoGeneIdentifyNo", con);
        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@flag", iFlag);

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 自动产生识别码并保存到公共识别码表
    /// <returns>返回是否成功</returns>
    public Boolean AddIntoPubIdentifys()
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddIntoPubIdentifys", con);
        com.CommandType = CommandType.StoredProcedure;
        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 修改当前的识别码
    /// <returns>返回是否成功</returns>
    /// strCurIdentifyNo, strUpdIdentifyNo
    public Boolean UpdateCurrentIdentify(string sCurIdentifyNo, string sUpdIdentifyNo)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_UpdateCurIdentify", con);
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@CurIdentifyNo", SqlDbType.NVarChar, 30);
        com.Parameters["@CurIdentifyNo"].Value = sCurIdentifyNo;
        com.Parameters.Add("@UpdIdentifyNo", SqlDbType.NVarChar, 30);
        com.Parameters["@UpdIdentifyNo"].Value = sUpdIdentifyNo;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 作废当前选中的识别码
    /// <returns>返回是否成功</returns>
    /// strCurIdentifyNo
    public Boolean DeleteCurrentIdentify(string sCurIdentifyNo)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_DeleteCurIdentify", con);
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@CurIdentifyNo", SqlDbType.NVarChar, 30);
        com.Parameters["@CurIdentifyNo"].Value = sCurIdentifyNo;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 作废临时的识别码表
    /// <returns>返回是否成功</returns>
    /// strCurIdentifyNo
    public Boolean DeleteTempIdentifys()
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_DeleteTempIdentifys", con);
        com.CommandType = CommandType.StoredProcedure;

        try
        {
            con.Open();
            com.ExecuteNonQuery();
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 保存前检查所产生的识别码是否与商品数量一致
    /// </summary>
    public int CheckIdentifyNos()
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_getGeneIdentifyNoCount", con);
        dat.SelectCommand.CommandType = CommandType.StoredProcedure;

        try
        {
            con.Open();
            int iIdenQty = int.Parse(dat.SelectCommand.ExecuteScalar().ToString().Trim());
            return iIdenQty;
        }
        catch
        {
            return 0;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 查询公共识别码表中是否已经生成过识别码
    /// </summary>
    public DataSet CheckIsGeneraTionIdentifys(string sBodNo, DateTime dBodDate, string sIm_no, string sBatch_no)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_CheckIsGeneraTion", con);
        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@bodNo", sBodNo);
        dat.SelectCommand.Parameters.Add("@bodDate", dBodDate);
        dat.SelectCommand.Parameters.Add("@im_no", sIm_no);
        dat.SelectCommand.Parameters.Add("@batch_no", sBatch_no);

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
        return dst;
    }

    /// <summary>
    /// 查询识别码
    /// </summary>
    public DataSet GetQueryGivenIdentifyNo(string sIdentifyNo, string sBodNo)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_QueryGivenIdentifyNo", con);
        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@IdentifyNo", sIdentifyNo);
        dat.SelectCommand.Parameters.Add("@BodNo", sBodNo);

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 根据商品编号查询所有相关的识别码
    /// </summary>
    public DataSet GetIdentifyNoByIm_no(string sIm_no)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_QueryIdentifyNoByIm_no", con);
        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@Im_no", sIm_no);

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }
//****************************************************选择识别码相关处理************************************************************
    /// <summary>
    /// 选择识别码后新增到公共识别码表
    /// <returns>返回是否成功</returns>
    /// strCurIdentifyNo, strUpdIdentifyNo
    public Boolean UpdatePubIdentifyNos(string sIdentifyNo, string sOutBod_no, string sOutBod_date)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_updatePubIdentifyNo", con);
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@identify_no", SqlDbType.NVarChar, 30);
        com.Parameters["@identify_no"].Value = sIdentifyNo;
        com.Parameters.Add("@outBod_no", SqlDbType.NVarChar, 30);
        com.Parameters["@outBod_no"].Value = sOutBod_no;
        com.Parameters.Add("@outBod_date", SqlDbType.DateTime, 8);
        com.Parameters["@outBod_date"].Value = sOutBod_date;

        try
        {
            con.Open();
            com.ExecuteNonQuery();
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 根据商品编号查询所有相关的识别码
    /// </summary>
    public DataSet GetExistIdentifyNos(string sOutBod_no,string sIm_no)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_QueryExistIdentifyNos", con);
        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@outBodNo", sOutBod_no);
        dat.SelectCommand.Parameters.Add("@Im_no", sIm_no);

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }
}
